// pages/coupon/coupon.js
import WxValidate from '../../utils/validate';
import Toast from '../../miniprogram_npm/@vant/weapp/toast/toast';
const FormData = require('../../utils/formData.js')
var app = getApp();
var inputContent = {};//表单输入内容
Page({
  data: {
      fileList: [
    ],
  },

  //获取用户输入
  bindChange: function (e) {
    inputContent[e.currentTarget.id] = e.detail.value
  },

  onLoad: function (options) {
    this.WxValidate = new WxValidate({
      yyzz: {  //验证规则 input name值
          required: true,
          maxlength: 20
      },
      company: {
          required: true,
          maxlength: 50
      },
      name: {
        required: true,
        maxlength: 20
      },
      phone: {
        required: true,
        tel:true
      },
    },
    {
      yyzz: { //提示信息
          required: "营业执照不能为空",
          maxlength: "输入信息过长"
      },
      company: { //提示信息
        required: "公司名称不能为空",
        maxlength: "输入信息过长"
      },
      name: { //提示信息
        required: "联系人不能为空",
        maxlength: "输入信息过长"
      },
      phone: { //提示信息
          required: "手机号不能为空",
          tel: "请填写正确的手机号"
      }
    })
  },

  couponSubmit:function(e){
    const params = e.detail.value
    if (!this.WxValidate.checkForm(e)) {
      const error = this.WxValidate.errorList
      wx.showModal({
          title: '提示',
          content: error[0].msg,
          showCancel: false,
          confirmColor: "#f26604",
          success: function (res) {
              if (res.confirm) {
                  console.log('用户点击确定');
              }
          }
      })
      console.log(error)
      return false
    }
    let formData = new FormData();
    formData.append("yyzz", inputContent.yyzz);
    formData.append("company", inputContent.company);
    formData.append("name", inputContent.name);
    formData.append("phone", inputContent.phone);
    formData.append("token",wx.getStorageSync('token'))
    if(typeof(this.data.fileList[0]) != "undefined"){
      formData.appendFile("file", this.data.fileList[0].path);
    }
    let data = formData.getData();
    wx.request({
      url: "http://127.0.0.1:8000/coupon/",
      method: "POST",
      // data: {//get 方法下的数据格式
      //   yyzz:inputContent.yyzz,
      //   company:inputContent.company,
      //   name:inputContent.name,
      //   phone:inputContent.phone
      // },
      data:data.buffer,
      header: {
          'content-type': data.contentType//"json"
      },
      success: function (res) {
        if (res.data.status > 0) {
          Toast(res.data.msg)
          setTimeout(function(){
            wx.navigateBack({delta: 1})
          },500)
        }else if(res.data.status == -1){
          app.login(function (userInfo) {
            Toast("登录成功，请重新提交")
          })
        }
      },
      fail: function (error) {
        wx.showToast({
            title: "请求失败:",
            icon: 'success',
            duration: 2000
        })
        console.log(error)
      },
      complete: function () {
      }
    })
  },

  // 点击文件上传后的处理动作
  afterRead(event) {
    let {file}  = event.detail;//指定只取event.detail里key为的file的字典
    const { fileList = [] } = this.data;//只取this.data里的fileList字典，如果为空则初始化为[]空数组
    fileList.push({ ...file, url: file.path });//push file的所有内容，再添加一个url的内容
    this.setData({ fileList });
  },

  // 删除已经上传的图片
  deleteImage(event){
    const { fileList = [] } = {};
    this.setData({ fileList });
  }
})